﻿@section js{
    <script src="~/Scripts/Home/Login/loginTop.js"></script>
}
<div style="width: 100%; height: 100%">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="white-space: nowrap;">
                    <a class="mini-button" iconcls="icon-search" tooltip="搜索" plain="true" onclick="search()"></a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-add" onclick="addRow()" plain="true">增加</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-remove" onclick="removeRow()" plain="true">删除</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-save" onclick="saveData()" plain="true">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-add" onclick="config()" plain="true">配置权限</a>
                </td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div id="grid" class="mini-datagrid" style="width: 100%; height: 100%;"
            allowresize="false" pagesize="20" sizelist="[5,10,20,50]" allowcellvalid="true"
            allowcelledit="true" allowcellselect="true" multiselect="true" oncellendedit="oncellendedit">
            <div property="columns">
                <div type="indexcolumn"></div>
                <div type="checkcolumn"></div>
                <div field="UCode" width="80" headeralign="center" allowsort="true">
                    用户编码
                       <input property="editor" class="mini-textbox" style="width: 100%;" />
                </div>
                <div field="UName" headeralign="center" allowsort="true">
                    用户名
                     <input property="editor" class="mini-textbox" style="width: 100%;" />
                </div>
                <div field="UPhone" width="100" headeralign="center" allowsort="true">
                    手机
                      <input property="editor" class="mini-textbox" style="width: 100%;" />
                </div>
                <div field="USex" width="80" headeralign="center" allowsort="true" renderer="sexRenderer">
                    性别
                    <input property="editor" class="mini-combobox" style="width: 100%;" valuefield="key" textfield="value" data="sex" />
                </div>
                <div field="CName" width="80" headeralign="center" allowsort="true">
                    <input property="editor" class="mini-treeselect" allowinput="true" valuefield="CID" displayfield="CName" textfield="CName" parentfield="FID"
                        virtualscroll="true" multiselect="false" url="/SystemData/Company/GetAllCompanyList" valuefromselect="false" />
                    公司
                </div>
                <div field="EMail" width="100" headeralign="center" allowsort="true">
                    邮箱
                    <input property="editor" class="mini-textarea" style="width: 100%;" minheight="80" />
                </div>
                <div field="WeChat" width="100" headeralign="center" allowsort="true">
                    微信
                    <input property="editor" class="mini-textbox" style="width: 100%;" />
                </div>
                <div field="QQ" width="100" headeralign="center" allowsort="true">
                    QQ
                    <input property="editor" class="mini-textbox" style="width: 100%;" />
                </div>
                <div field="Address" width="100" headeralign="center" allowsort="true">
                    地址
                    <input property="editor" class="mini-textarea" style="width: 100%;" minheight="80" />
                </div>
                <div field="IsValid" width="50" headeralign="center" allowsort="true" type="checkboxcolumn" @*truevalue="true" falsevalue="false"*@>
                    有效
                </div>
                <div field="IsInner" width="50" headeralign="center" allowsort="true" type="checkboxcolumn" @*truevalue="true" falsevalue="false"*@>
                    内部用户
                </div>
                <div field="IsAdmin" width="50" headeralign="center" allowsort="true" type="checkboxcolumn"@* truevalue="true" falsevalue="false"*@>
                    管理员
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();

    var sex = [{ key: "true", value: "男" }, { key: "false", value: "女" }];

    var grid = mini.get("grid");
    grid.setUrl("/SystemData/User/GetAllUsers");
    grid.load();

    function sexRenderer(e) {
        switch (e.value.toString()) {
            case "0":
            case "false": { return "女"; break; }
            case "1":
            case "true": { return "男"; break; }
            default: { return "不详"; break; }
        }
    }

    function oncellendedit(e) {
        if (e.field == "CName") {
            grid.updateRow(e.record, { CName: e.editor.text, CID: e.editor.value });
        }
    }

    function addRow() {
        var row = { IsAdmin: 0, IsValid: 1, IsInner: 1, USex: true, PWD: "123456" };
        grid.addRow(row);
    }

    function removeRow() {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
    }

    function config() {
        var row = grid.getSelected();
        var rows = grid.getSelecteds();
        if (!row || rows.length > 1) {
            $.tip({ text: "请选择一行", staytime: 1000 });
            return;
        }
        if (row.UID == null) {
            $.tip({ text: "请先保存", staytime: 1000 });
            return;
        }
        mini.open({
            url: "/SystemData/User/User_Menu",
            width: 800,
            height: 500,
            title: "配置权限",
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { UID: row.UID };
                iframe.contentWindow.setData(data);
            },
            ondestroy: function (action) {
            }
        });
    }

    function saveData() {
        grid.validate();
        if (grid.isValid() == false) {
            var error = grid.getCellErrors()[0];
            grid.beginEditCell(error.record, error.column);
            return;
        }

        var added = grid.getChanges("added");
        var jsonAdd = mini.encode(added);

        var modified = grid.getChanges("modified");
        var jsonEdit = mini.encode(modified);

        var removed = grid.getChanges("removed");
        var jsonRemove = mini.encode(removed);

        grid.loading("保存中，请稍后......");
        $.ajax({
            url: "/SystemData/User/SaveData",
            data: { add: jsonAdd, edit: jsonEdit, remove: jsonRemove },
            type: "post",
            success: function (text, flag, res) {
                grid.unmask();
                if (res.responseText == "") {
                    grid.reload();
                }
                else
                    mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                mini.alert(errorThrown.responseText);
            }
        });
    }

</script>
